package me.zhanshi123.lab4.service.impl;

import me.zhanshi123.lab4.dao.UserDao;
import me.zhanshi123.lab4.entity.ApiResponse;
import me.zhanshi123.lab4.entity.AuthIdentity;
import me.zhanshi123.lab4.entity.User;
import me.zhanshi123.lab4.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService implements IUserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    public User getUser(String name) {
        return userDao.getUser(name);
    }

    public int addUser(String name, String password, String email, int group) {
        String encoded = passwordEncoder.encode(password);
        return userDao.addUser(name, encoded, group, email);
    }

    public int updateUser(User user) {
        return userDao.updateUser(user.getName(), user.getPassword(), user.getGroup(), user.getEmail());
    }

    public String updateSelf(AuthIdentity authIdentity, String email) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        String name = authIdentity.getName();
        User user = getUser(name);
        if (user == null) {
            return apiResponse.fail("找不到用户");
        }
        user.setEmail(email);
        if (updateUser(user) == 0) {
            return apiResponse.fail("数据库更新失败");
        }
        return apiResponse.success("ok");
    }

    public String updateOther(AuthIdentity authIdentity, String user) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        if (authIdentity.getGroup() == 0) {
            return apiResponse.fail("你的权限不足");
        }
        User user2 = getUser(user);
        if (user2 == null) {
            return apiResponse.fail("操作用户不存在");
        }
        if (user2.getGroup() == 0) {
            user2.setGroup(4);
        } else {
            user2.setGroup(0);
        }
        if (userDao.updateUser(user2.getName(), user2.getPassword(), user2.getGroup(), user2.getEmail()) == 0) {
            return apiResponse.fail("数据库异常");
        }
        return apiResponse.success("ok");
    }

    public String getInfo(AuthIdentity authIdentity) {
        ApiResponse<User> apiResponse = new ApiResponse<>();
        User user = getUser(authIdentity.getName());
        if (user == null) {
            return apiResponse.fail("找不到用户");
        }
        return apiResponse.success(user);
    }

    @Override
    public String getAll() {
        ApiResponse<List<User>> apiResponse = new ApiResponse<>();
        List<User> users = userDao.getAll();
        return apiResponse.success(users);
    }

    public boolean checkPassword(User user, String password) {
        return passwordEncoder.matches(password, user.getPassword());
    }

    @Override
    public String add(AuthIdentity authIdentity, String name, String password, String email) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        if (authIdentity.getGroup() < 4) {
            return apiResponse.fail("权限不足");
        }
        User user = getUser(name);
        if (user != null) {
            return apiResponse.fail("用户已存在");
        }
        if (addUser(name, password, email, 0) == 0) {
            return apiResponse.fail("数据库错误");
        }
        return apiResponse.success("ok");
    }

    public String changePassword(AuthIdentity authIdentity, String name, String password) {
        ApiResponse<String> apiResponse = new ApiResponse<>();
        if (authIdentity.getGroup() != 4) {
            return apiResponse.fail("权限不足");
        }
        User user = getUser(name);
        user.setPassword(passwordEncoder.encode(password));
        return apiResponse.success("ok");
    }
}
